#ifndef QTBOX2DABSTRACTITEM_H
#define QTBOX2DABSTRACTITEM_H

#include <QDataStream>

#include "QtBox2DBaseItem.h"
#include "Box2D/Box2D.h"
#include "qtpropertymanager.h"
#include "qteditorfactory.h"
#include "qttreepropertybrowser.h"
#include "qtbuttonpropertybrowser.h"
#include "qtgroupboxpropertybrowser.h"
#include "qtvariantproperty.h"

class QtBox2DView;
class QtBox2DScene;

class QtBox2DAbstractItem : public QObject,
        public QtBox2DBaseItem
{
    Q_OBJECT
	Q_PROPERTY(QPointF pos READ pos WRITE setPos)
	Q_PROPERTY(qreal rotation READ rotation WRITE setRotation)
	Q_PROPERTY(QSizeF mTexSize READ texSize WRITE setTexSize)
	Q_PROPERTY(QString mTexName READ texName WRITE setTexName)
	Q_PROPERTY(bool mIsDrawFrame READ isDrawFrame WRITE setDrawFrame)
public:
    explicit QtBox2DAbstractItem(QtBox2DScene* scene);

    virtual ~QtBox2DAbstractItem(void);

	virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

	virtual void getBodyFixture( b2BodyDef& bodyDef, b2FixtureDef& fixtureDef );
    virtual void createBody() = 0;

	virtual QRectF boundingRect() const;
	virtual void createPropertyWidget();
	void connectProperty();
	virtual QWidget* propertyWidget() { return mPropertyWidget; }
	virtual void createControllPoints() = 0;
	virtual void resetControllPoints() = 0;
	virtual void showControllPoints(bool bShow);
	//virtual QString  getName() const = 0;

	//new properties
	const QSizeF    texSize() const { return mTexSize; }
	void setTexSize(const QSizeF& texSize) ;
	const QString    texName() const {return mTexName; }
	void setTexName(const QString& texName); 
	bool isDrawFrame() const { return mIsDrawFrame; };
	void setDrawFrame(bool bDrawFrame);
	
	void setProperty(const QString&	propertyName, const QVariant& value);


signals:
	void propertyChanged(QtProperty *, const QVariant &);
protected:
	void connectManager();
	void disconnectManager();
	void emitPropertyChanged(QtProperty *prop, const QVariant &value)
	{
		disconnectManager();
		emit propertyChanged(prop, value);
		connectManager();
	}
public slots:
	virtual void start(bool bStart);
	virtual void bodyUpdate();
	virtual void setProperty(QtProperty *prop, const QVariant &value);

	virtual void save(QDataStream &dataStream);
	virtual void load(QDataStream &dataStream);
	friend QDataStream & operator << (QDataStream &dataStream, QtBox2DAbstractItem &itemBase);
	friend QDataStream & operator >> (QDataStream &dataStream, QtBox2DAbstractItem &itemBase);

protected:
	virtual void	mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
	virtual void	mousePressEvent ( QGraphicsSceneMouseEvent * event );
	virtual void    mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
protected:
	b2Body* mBody;
	b2World* mWorld;
	bool    mIsStart;

	QSizeF  mTexSize;
	QString mTexName;
	QPixmap mTexture;

	bool    mIsDrawFrame;

	QtTreePropertyBrowser* mPropertyWidget;
	QtVariantPropertyManager* mVariantManager;
	QtVariantProperty*        mFrameProperty;
	QtVariantProperty*        mPosProperty;
	QtVariantProperty*        mRotProperty;
	QtVariantProperty*        mTexProperty;
	QtVariantProperty*        mTexSizeProperty;

	QtVariantProperty*        mLinearVelProperty;
	QtVariantProperty*        mAngleVelProperty;
	QtVariantProperty*        mLinearDamping;
	QtVariantProperty*        mAngleDamping;
	QtVariantProperty*        mAllowSleep;
	QtVariantProperty*        mAwake;
	QtVariantProperty*        mFixedRotation;
	QtVariantProperty*        mType;
	QtVariantProperty*        mActive;
	QtVariantProperty*        mBullet;
	QtVariantProperty*        mInertiaScale;

	QtVariantProperty*        mFriction;
	QtVariantProperty*        mRestitution;
	QtVariantProperty*        mDensity;
	QtVariantProperty*        mCategoryBits;
	QtVariantProperty*        mMaskBits;
	QtVariantProperty*        mGroupIndex;
	QtVariantProperty*        mIsSensor;

private:
};

#endif // QTBOX2DABSTRACTITEM_H
